<template>
  <div>
    <div class="udlite-container partners">
      <van-cell-group>
        <van-field
          type="password"
          v-model="password"
          label="原密码"
          placeholder="请输入原密码"
        ></van-field>
        <van-field
          type="password"
          v-model="new_password"
          label="新密码"
          placeholder="请输入新密码"
        ></van-field>
        <van-field
          type="password"
          v-model="verify_new_password"
          label="确认新密码"
          placeholder="请确认新密码"
        ></van-field>
      </van-cell-group>
      <van-button type="primary" @click="change_password">提交修改</van-button>
    </div>

    <hr>
    
    <div class="udlite-container partners">
      <van-cell-group>
        <van-field
         v-model="cate"
         label="名称"
         placeholder="请输入课程名程"
        ></van-field>

        所属课程类别
        <select v-model="pid">
          <option v-for="child in c_list" :key="child.id" :value="child.id">
            {{child.name}}
          </option>
        </select>
      </van-cell-group>
      &emsp;
      <div>
        <van-button round type="primary" @click="add_cate">添加课程</van-button>
        <van-button round type="info" to="/showcate">查看课程</van-button>
      </div>
    </div>

    <projectfooter />
  </div>
</template>

<script>
import projectfooter from "../header_folder/projectfooter.vue";
export default {
  components: { projectfooter },
  data() {
    return {
      password: "",
      new_password: "",
      verify_new_password: "",

      c_list:[],
      cate:"",
      pid:"",
      uid: localStorage.getItem("uid"),
    };
  },
  methods: {                  
    change_password() {
      var token = localStorage.getItem("token");
      var username = localStorage.getItem("u_name");
      this.axios
        .put("http://127.0.0.1:8000/center/", {
          token: token,
          username: username,
          password: this.password,
          new_password: this.new_password,
        })
        .then((res) => {
          if (this.password == "") {
            this.$toast("原密码不能为空");
            return false;
          } else if (this.new_password == "") {
            this.$toast("新密码不能为空");
            return false;
          } else if (this.new_password == this.password) {
            this.$toast("新密码不能与原密码一致");
            return false;
          } else if (this.verify_new_password == "") {
            this.$toast("确认密码不能为空");
            return false;
          } else if (this.verify_new_password != this.new_password) {
            this.$toast("两次输入的新密码不一致");
            return false;
          }
          if (res.data.code == 200) {
            this.$toast(res.data.msg);
            this.$router.push("/mylogin");
          } else {
            this.$toast(res.data.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    add_cate(){
      this.axios.post("http://127.0.0.1:8000/cate/",{name:this.cate,pid:this.pid,uid:this.uid})
        .then((res) => {
          console.log(res.data.msg);
        })
        .catch(err=>{
            console.log(err)
        })
    }
  },
  created() {
    this.axios.get("http://127.0.0.1:8000/cate/")
      .then((res) => {
        console.log(res.data.msg);
        this.c_list = res.data.data;
      })
      .catch(err=>{
          console.log(err)
      })
  },
};
</script>

<style>
</style>